package edu.java.homework.hw09.exercises.salads.salads;

import edu.java.homework.hw09.exercises.salads.components.Components;
import edu.java.homework.hw09.exercises.salads.components.EggComponent;
import edu.java.homework.hw09.exercises.salads.components.FruitComponent;
import edu.java.homework.hw09.exercises.salads.components.MeatComponent;
import edu.java.homework.hw09.exercises.salads.components.MilkComponent;
import edu.java.homework.hw09.exercises.salads.components.SpiceComponent;
import edu.java.homework.hw09.exercises.salads.components.VegetableComponent;

public abstract class Salad {

    // --- Instance Variables --------------------------------------------------
    private Components components = null;
    private int price = 0;

    // --- Constructors --------------------------------------------------------
    public Salad() {
	this(null, 0);
    }

    public Salad(Components components) {
	this(components, 0);
    }

    public Salad(Components components, int price) {
	this.components = components;
	this.price = price;
    }

    // --- Methods -------------------------------------------------------------

    public abstract void prepareComponents();

    public String getSaladName() {
	return "Salad";
    }

    @Override
    public String toString() {
	StringBuilder sb = new StringBuilder();
	sb.append(getSaladName() + "\n");
	sb.append(components.toString());
	setPrice(calculateSaladPrice());
	sb.append("Salad price: " + getPrice() + "\n");
	return sb.toString();
    }

    public int calculateSaladPrice() {
	int sum = 0;

	VegetableComponent[] vegetables = this.getComponents()
		.getVegetableComponents();
	if (vegetables != null) {
	    for (VegetableComponent veg : vegetables) {
		// System.out.println(veg.getVegetable().toString());
		sum += veg.getVegetable().getPrice() * veg.getWeight();
	    }
	}
	FruitComponent[] fruits = components.getFruitComponents();
	if (fruits != null) {
	    for (FruitComponent fruit : fruits) {
		// System.out.println(fruit.getFruit().toString());
		sum += fruit.getFruit().getPrice() * fruit.getWeight();
	    }
	}
	MeatComponent[] meatComponents = components.getMeatComponents();
	if (meatComponents != null) {
	    for (MeatComponent meat : meatComponents) {
		// System.out.println(meat.getMeatProduct().toString());
		sum += meat.getMeatProduct().getPrice() * meat.getWeight();
	    }
	}
	MilkComponent[] milkComponents = components.getMilkComponents();
	if (milkComponents != null) {
	    for (MilkComponent milk : milkComponents) {
		// System.out.println(milk.getMilkProduct().toString());
		sum += milk.getMilkProduct().getPrice() * milk.getWeight();
	    }
	}
	EggComponent[] eggs = components.getEggComponents();
	if (eggs != null) {
	    for (EggComponent egg : eggs) {
		// System.out.println(egg.getEggProduct().toString());
		sum += egg.getEggProduct().getPrice() * egg.getWeight();
	    }
	}
	SpiceComponent[] spices = components.getSpiceComponents();
	if (spices != null) {
	    for (SpiceComponent spice : spices) {
		// System.out.println(spice.getSpice().toString());
		sum += spice.getSpice().getPrice() * spice.getWeight();
	    }
	}
	return sum;
    }

    // --- Methods -> Getters and Setters --------------------------------------

    public Components getComponents() {
	return components;
    }

    public void setComponents(Components components) {
	this.components = components;
    }

    public int getPrice() {
	return price;
    }

    public void setPrice(int price) {
	// check input parameters
	if (price < 0) {
	    throw new IllegalArgumentException(
		    "Incorrect input. The price should be 0 or positive integer number");
	}
	this.price = price;
    }

}
